package com.thinkgem.jeesite.common.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Component;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.context.support.XmlWebApplicationContext;

import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.pro.entity.ProjectUser;
import com.thinkgem.jeesite.modules.pro.entity.TaskGroup;
import com.thinkgem.jeesite.modules.pro.service.ProjectTaskService;
import com.thinkgem.jeesite.modules.pro.service.ProjectUserService;
import com.thinkgem.jeesite.modules.pro.service.TaskGroupService;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;

/**
 * 项目管理权限控制过滤器
 * @author fanql 2017年6月27日
 *
 */
@Component
public class ProjectFilter implements Filter{
	private ProjectUserService projectUserService;
	private TaskGroupService taskGroupService;
	private ProjectTaskService projectTaskService;
	
	public ProjectUserService getProjectUserService() {
		return projectUserService;
	}

	public void setProjectUserService(ProjectUserService projectUserService) {
		this.projectUserService = projectUserService;
	}

	@Override
	public void destroy() {
		
	}

	@Override
	public void doFilter(ServletRequest req, ServletResponse resp, FilterChain filterChain)
			throws IOException, ServletException {
		HttpServletRequest request = (HttpServletRequest) req;
		HttpServletResponse response = (HttpServletResponse) resp;
		String url = request.getRequestURI();
		String projectId = request.getParameter("projectId");
		ServletContext sc = request.getSession().getServletContext();
		XmlWebApplicationContext cxt = (XmlWebApplicationContext)WebApplicationContextUtils.getWebApplicationContext(sc);
		if(cxt != null && cxt.getBean("projectUserService") != null && projectUserService == null){
			projectUserService = (ProjectUserService) cxt.getBean("projectUserService");
		}
		if(cxt != null && cxt.getBean("taskGroupService") != null && taskGroupService == null){
			taskGroupService = (TaskGroupService) cxt.getBean("taskGroupService");
		}
		if(cxt != null && cxt.getBean("projectTaskService") != null && projectTaskService == null){
			projectTaskService = (ProjectTaskService) cxt.getBean("projectTaskService");
		}
		if (StringUtils.isEmpty(projectId)) {
			projectId = request.getParameter("project.id");
		}
		if (StringUtils.isEmpty(projectId)) {
			String taskGroupId = request.getParameter("taskGroup.id");
			if (StringUtils.isEmpty(taskGroupId)) {
				taskGroupId = request.getParameter("taskGroupId");
			}
			TaskGroup taskGroup = taskGroupService.get(taskGroupId);
			if (null != taskGroup && null != taskGroup.getProject()) {
				projectId = taskGroup.getProject().getId();
			}
		}
		if (StringUtils.isEmpty(projectId)) {
			projectId = request.getParameter("id");
		}
		User user = UserUtils.getUser();
		ProjectUser param = new ProjectUser();
		param.setUser(user);
		param.setProjectId(projectId);
		ProjectUser projectUser = projectUserService.getByParam(param);
		boolean flag = true;
		//拦截逻辑
		//项目修改，授权码为16
		//PropertiesLoader propertiesLoader = new PropertiesLoader("/jeesite.properties");
		//String adminPath = propertiesLoader.getProperty("adminPath");
		if (url.indexOf("project/save") > 0 && (null == projectUser || (projectUser.getCode() & 16) != 16) && !StringUtils.isEmpty(projectId)) {
			flag = false;
		}
		//添加成员，授权码为8
		if (url.indexOf("project/assignrole") > 0 && (null == projectUser || (projectUser.getCode() & 8) != 8)) {
			flag = false;
		}
		//移除人员，授权码为4
		if (url.indexOf("project/outProject") > 0 && (null == projectUser || (projectUser.getCode() & 4) != 4)) {
			flag = false;
		}
		//任务组修改，授权码为2
		if (url.indexOf("taskGroup/save") > 0 && (null == projectUser || (projectUser.getCode() & 2) != 2)) {
			flag = false;
		}
		//任务修改，授权码为1
		if (url.indexOf("projectTask/save") > 0) {
			/*String taskId = request.getParameter("id");
			ProjectTask projectTask = projectTaskService.get(taskId);
			TaskGroup taskGroup = taskGroupService.get(projectTask.getTaskGroup().getId());
			param.setProjectId(taskGroup.getProject().getId());
			projectUser = projectUserService.getByParam(param);*/
			if (null == projectUser || (projectUser.getCode() & 1) != 1) {
				flag = false;
			}
		}
		if (flag) {
			filterChain.doFilter(req, resp);
		}else {
			request.getRequestDispatcher("modules/pro/noAuth").forward(req, resp);
		}
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub
		
	}

	public TaskGroupService getTaskGroupService() {
		return taskGroupService;
	}

	public void setTaskGroupService(TaskGroupService taskGroupService) {
		this.taskGroupService = taskGroupService;
	}

	public ProjectTaskService getProjectTaskService() {
		return projectTaskService;
	}

	public void setProjectTaskService(ProjectTaskService projectTaskService) {
		this.projectTaskService = projectTaskService;
	}

}
